﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Hive.Core.Model
{
    /// <summary>
    /// Default implementation of a package.
    /// </summary>
    public class DefaultPackage : IPackage
    {
        private readonly DefaultHive hive;
        private readonly DirectoryInfo directory;

        /// <summary>
        /// Create a package.
        /// </summary>
        /// <param name="hive">The hive.</param>
        /// <param name="directory">The package directory.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="hive"/> or <paramref name="directory"/> is null.</exception>
        public DefaultPackage(DefaultHive hive, DirectoryInfo directory)
        {
            if (hive == null)
                throw new ArgumentNullException("hive");
            if (directory == null)
                throw new ArgumentNullException("directory");

            this.hive = hive;
            this.directory = directory;
        }

        /// <inheritdoc />
        public IHive Hive
        {
            get { return hive; }
        }

        /// <inheritdoc />
        public string Name
        {
            get { return directory.Name; }
        }

        /// <inheritdoc />
        public DirectoryInfo Directory
        {
            get { return directory; }
        }

        /// <inheritdoc />
        public IList<IPackageBranch> GetBranches()
        {
            return directory.GetDirectories().Select(x => new DefaultPackageBranch(this, x)).ToArray();
        }

        /// <inheritdoc />
        public IPackageBranch GetBranch(string branchName)
        {
            if (branchName == null)
                throw new ArgumentNullException("branchName");

            return new DefaultPackageBranch(this, new DirectoryInfo(Path.Combine(directory.FullName, branchName)));
        }
    }
}
